<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/13/0013
 * Time: 9:43
 */

namespace app\sports_admin\service;

use app\exception\SuggestionTypeException;
use app\sports_admin\model\SuggestionType as SuggestionTypeModel;
use think\Request;
use think\Route;

class SuggestionType
{
    /**
     * 添加类别
    */
    public static function typeInsert()
    {
        //获取字段信息参数
        $params = self::getParams();
        $bool = SuggestionTypeModel::create($params);
        if(!$bool){
            throw new SuggestionTypeException([
                'msg' => '添加失败',
                'errorCode' => 400
            ]);
        }
        return json(['code'=>201,'msg'=>'添加成功']);
    }
    /**
     * 修改类别
    */
    public static function typeUpdate()
    {
        //类别信息id
        $id = intval(input('post.id'));
        //获取字段信息参数
        $params = self::getParams();
        $bool = SuggestionTypeModel::where('id',$id)->update($params);
        if(!$bool){
            throw new SuggestionTypeException([
                'msg' => '修改失败',
                'errorCode' => 400
            ]);
        }
        return json(['code'=>201,'msg'=>'修改成功']);
    }
    /**
     * 获取参数
    */
    public static function getParams()
    {
        //类别名称
        $params = Request::instance()->only(['title','path','parent_id']);
        $info = SuggestionTypeModel::where('title',$params['title'])->find();
        if($info){
            throw new SuggestionTypeException([
                'msg' => '类别已存在',
                'errorCode' => 400
            ]);
        }
        return $params;
    }
    //
    public static function addParentType()
    {
        $parent_id = "0";
        $path      = "0,";
        $createTime= time();
        $data = Request::instance()->only(['title']);

        return $info = SuggestionTypeModel::insert(["title"=>$data['title'],"parent_id"=>$parent_id,"path"=>$path,"create_time"=>$createTime]);


    }
}